//
//  BDCouponValidationView.m
//  BadBoy
//
//  Created by wen on 2017/7/11.
//  Copyright © 2017年 com.8xiong. All rights reserved.
//

#import "BDCouponValidationView.h"

@interface BDCouponValidationView ()

@property (nonatomic, strong) UIView *bkView;
@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) BDMyCouponsModel *couponsModel;

@end

@implementation BDCouponValidationView

+ (instancetype)couponValidationViewWithCoupons:(BDMyCouponsModel *)coupons{
    BDCouponValidationView *view = [[BDCouponValidationView alloc] initWithFrame:CGRectMake(0, 0, kBDScreenWidth, kBDScreenHeight) coupons:coupons];
    return view;
}

- (instancetype)initWithFrame:(CGRect)frame coupons:(BDMyCouponsModel *)coupons{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor clearColor];
        self.couponsModel = coupons;
        [self addSubview:self.bkView];
        [self addSubview:self.contentView];
        _contentView.center = CGPointMake(self.width / 2.0, self.height / 2.0);
    }
    return self;
}

- (void)show{
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [UIView animateWithDuration:0.3 animations:^{
        self.bkView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6];
        self.contentView.alpha = 1;
    }];
}

- (void)hidden{
    [UIView animateWithDuration:0.3 animations:^{
        self.bkView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0];
        self.contentView.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (UIView *)bkView{
    if (!_bkView) {
        self.bkView = [[UIView alloc] initWithFrame:self.bounds];
        _bkView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
        [_bkView addGestureRecognizer:tap];
        [[tap rac_gestureSignal] subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
            [self hidden];
        }];
    }
    return _bkView;
}

- (UIView *)contentView{
    if (!_contentView) {
        self.contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width - 80, 0)];
        _contentView.backgroundColor = [UIColor whiteColor];
        _contentView.alpha = 0;
        
        UILabel *titlelLabel = [[UILabel alloc] init];
        titlelLabel.font = [UIFont systemFontOfSize:20];
        titlelLabel.textColor = [UIColor colorWithHexString:@"#1c1c1c"];
        titlelLabel.text = _couponsModel.title;
        [titlelLabel sizeToFit];
        titlelLabel.top = 30;
        titlelLabel.centerX = _contentView.width / 2.0;
        [_contentView addSubview:titlelLabel];
        
        NSString *jsonString = [NSString stringWithFormat:@"beboy://code/%@", @(_couponsModel.code)];
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 150, 150)];
        imageView.image = [UIImage qrImageForString:jsonString imageSize:150 logoImageSize:0];
        imageView.top = titlelLabel.bottom + 15;
        imageView.centerX = _contentView.width / 2.0;
        [_contentView addSubview:imageView];
        
        UILabel *tishiLabel = [[UILabel alloc] init];
        tishiLabel.font = [UIFont systemFontOfSize:13];
        tishiLabel.textColor = [UIColor colorWithHexString:@"#a5a5a5"];
        tishiLabel.text = ASLocalizedString(@"您的专属验证码");
        [tishiLabel sizeToFit];
        tishiLabel.top = imageView.bottom + 20;
        tishiLabel.centerX = _contentView.width / 2.0;
        [_contentView addSubview:tishiLabel];
        
        UILabel *codeLabel = [[UILabel alloc] init];
        codeLabel.font = [UIFont systemFontOfSize:17];
        codeLabel.textColor = [UIColor colorWithHexString:@"#1c1c1c"];
        codeLabel.text = [NSString stringWithFormat:@"%@", @(_couponsModel.code)];
        [codeLabel sizeToFit];
        codeLabel.top = tishiLabel.bottom + 20;
        codeLabel.centerX = _contentView.width / 2.0;
        [_contentView addSubview:codeLabel];
        
        UILabel *bottomLabel = [[UILabel alloc] init];
        bottomLabel.font = [UIFont systemFontOfSize:13];
        bottomLabel.textColor = [UIColor colorWithHexString:@"#a5a5a5"];
        bottomLabel.text = ASLocalizedString(@"您可选择扫上面二维码或输入下面验证码进行验证");
        [bottomLabel sizeToFit];
        bottomLabel.numberOfLines = 0;
        bottomLabel.textAlignment = NSTextAlignmentCenter;
        bottomLabel.width = MIN(bottomLabel.width, _contentView.width - 60);
        bottomLabel.height = MAX(bottomLabel.height, [bottomLabel.text heightWithFont:bottomLabel.font width:bottomLabel.width]);
        bottomLabel.top = codeLabel.bottom + 20;
        bottomLabel.centerX = _contentView.width / 2.0;
        [_contentView addSubview:bottomLabel];
        
        _contentView.height = bottomLabel.bottom + 25;
        
        _contentView.layer.masksToBounds = YES;
        _contentView.layer.cornerRadius = 3;
    }
    return _contentView;
}


@end
